﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AiFrame.Interface.WinUI.Windows;
using AiFrame.InterfaceLib.MVP;
using AiFrame.InterfaceLib.Resources;
using AiFrameDemo.CustomerData.Core;
using AiFrameDemo.CustomerData.Core.ViewModel;
using AiFrameDemo.CustomerData.Core.Views;
using AiFrameDemo.Lib.Model;
using AiFrame.Interface.WinUI.Windows;

namespace AiFrameDemo.CustomerData.WinForms.Windows
{
    public partial class CustomerDataFrame : UserControl, ICustomerDataView
    {
        private CustomerDataViewModel _viewModel;
        private BindingSource _bindingSource;
        private ErrorProvider _errorProvider;
        private CustomerTabSubWindowContainer _subWindowContainer;


        public CustomerDataFrame()
        {
            InitializeComponent();
            Dock = DockStyle.Fill;
            _bindingSource = new BindingSource();

            _errorProvider = new ErrorProvider();
            _errorProvider.ContainerControl = this;
            _errorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;
            _errorProvider.DataSource = _bindingSource;
            
        }

        private void ShowToolbar()
        {
            ResourceImages resourceImages = new ResourceImages();

            ToolStrip toolStripButtons = new ToolStrip();
            ToolStripButton newButton = new ToolStripButton("Add new customer...", resourceImages.DatasetAdd, OnToolItemClick);
            newButton.Tag = "New";
            toolStripButtons.Items.Add(newButton);

            ToolStripButton openButton = new ToolStripButton("Show...", resourceImages.DatasetOpen, null);
            openButton.Tag = "Open";
            toolStripButtons.Items.Add(openButton);

            ToolStripButton deleteButton = new ToolStripButton("Delete", resourceImages.DatasetDelete, null);
            deleteButton.Tag = "Delete";
            toolStripButtons.Items.Add(deleteButton);

            ToolStripButton saveButton = new ToolStripButton("Save", null, OnToolItemClick);
            saveButton.Tag = "Save";
            toolStripButtons.Items.Add(saveButton);

            CustomerDataRegistry.Instance.CoreInterface.ToolBarManager.AddToolGroup(toolStripButtons, false, 1);
        }

        private void OnToolItemClick(object sender, EventArgs e)
        {
            switch((sender as ToolStripItem).Tag.ToString())
            {
                case "New":
                    _viewModel.UserWantsToAddCustomer();
                    break;
                case "Save":
                    _viewModel.UserWantsToSaveCustomer();
                    break;

            }
        }
        

        public void RefreshView()
        {
            // Do nothing
        }

        public void SetFree()
        {
            this.Dispose();
        }

        public void SetParentWindow(Control parent)
        {
            this.Parent = parent;
        }

        public void BringWindowToFront()
        {
            BringToFront();
            ShowToolbar();
        }

        public string GetCaption()
        {
            return "Kundendaten";
        }

        public void BindToViewModel(CustomerDataViewModel customerDataViewModel)
        {
            _viewModel = customerDataViewModel;
            _bindingSource.DataSource = _viewModel;

            BindingHelper.SetBinding(customerNumberTb, "Text", _bindingSource, "CustomerNumber", true, DataSourceUpdateMode.OnPropertyChanged);
            BindingHelper.SetBinding(customerFirstnameTb, "Text", _bindingSource, "Firstname", true, DataSourceUpdateMode.OnPropertyChanged);
            BindingHelper.SetBinding(customerLastnameTb, "Text", _bindingSource, "Lastname", true, DataSourceUpdateMode.OnPropertyChanged);
            BindingHelper.SetBinding(customerBirthdayTb, "Text", _bindingSource, "Birthday", true, DataSourceUpdateMode.OnPropertyChanged, true);
            BindingHelper.SetBinding(currentCustomerLabel, "Text", _bindingSource, "CurrentCustomerDisplay", true, DataSourceUpdateMode.OnPropertyChanged, true);

            allCustomersCmb.DataSource = _viewModel.CustomerListValues;
            allCustomersCmb.DisplayMember = "Value";
            allCustomersCmb.ValueMember = "Key";
            BindingHelper.SetBinding(allCustomersCmb, "SelectedValue", _bindingSource, "Customer", true, DataSourceUpdateMode.OnPropertyChanged);
            

            _subWindowContainer = new CustomerTabSubWindowContainer(pluginTabControl, _viewModel);
            CustomerDataRegistry.Instance.CoreInterface.PluginManager.InvokeNewDatasetAssignedEvent<Customer>(_subWindowContainer);
        }

        public ISubWindowContainer<Customer> SubWindowContainer
        {
            get { return _subWindowContainer; }
        }

        public void ResetCustomersDataSource()
        {
            allCustomersCmb.DataSource = null;
            allCustomersCmb.DisplayMember = "Value";
            allCustomersCmb.ValueMember = "Key";
//            allCustomersCmb.Items.Clear();
            allCustomersCmb.DataSource = _viewModel.CustomerListValues;
        }

        public void InformUser(string message)
        {
            MessageBox.Show(message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void DisplayError(string message)
        {
            MessageBox.Show(message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    class CustomerTabSubWindowContainer: TabControlSubWindowContainer<Customer>
    {
        public CustomerTabSubWindowContainer(TabControl tabControl, IDataSourceProvider<Customer> dataSourceProvider) : base(tabControl, dataSourceProvider)
        {
        }

        public override string ID
        {
            get { return "EDIT_CUSTOMER"; }
        }
    }
}
